﻿using System;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.MemberService
{
	public partial class BlackEdit : BasePage
	{
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00050009";//默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}


		protected void Page_Load(object sender, EventArgs e)
		{
			if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemType))
			{
				AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
				return;
			}

			string ID = Request.QueryString["id"];
			this.Hid_id.Value = ID;
			if (!IsPostBack)
				BindData(ID);
		}

		protected void BindData(string ID)
		{
			if (!string.IsNullOrWhiteSpace(ID))
			{
				int id = Convert.ToInt32(ID);
				TB_BlackList resultE = DAL.DALHelper.Instance.GetObject<TB_BlackList>("ID", id);
				this.Name.Text = resultE.PersonName;
				this.CID.Text = resultE.IDCard;
				this.Company.Text = resultE.CompanyName;
				this.Date.Text = resultE.AboutTime;
				this.Mone.Text = resultE.Description;
			}
		}

		protected void Subbtn_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrWhiteSpace(this.Hid_id.Value))
			{// 新增
				var NewBlack = DAL.DALHelper.Instance.Create<TB_BlackList>();

				NewBlack.PersonName = this.Name.Text.Trim();
				NewBlack.IDCard = this.CID.Text.Trim();
				NewBlack.CompanyName = this.Company.Text.Trim();
				NewBlack.AboutTime = this.Date.Text.Trim();
				NewBlack.Description = this.Mone.Text.Trim();
				NewBlack.CreateBy = AdminBasePage.AdminInfo.UserName;
				NewBlack.CreateDate = DateTime.Now;

				DAL.DALHelper.Instance.Add<TB_BlackList>(NewBlack);
				DAL.DALHelper.Instance.SaveChanges();

			}
			else
			{// 修改
				int id = Convert.ToInt32(this.Hid_id.Value);
				var result = DAL.DALHelper.Instance.GetObject<TB_BlackList>("ID", id);

				result.PersonName = this.Name.Text.Trim();
				result.IDCard = this.CID.Text.Trim();
				result.CompanyName = this.Company.Text.Trim();
				result.AboutTime = this.Date.Text.Trim();
				result.Description = this.Mone.Text.Trim();

				DAL.DALHelper.Instance.Update<TB_BlackList>(result);
				DAL.DALHelper.Instance.SaveChanges();

			}

			RefreshNewsList();
		}

		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList();";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}
	}
}